#include <reg51.h>  	
#include"lcd.h"
#define SELECT 1

sbit buz=P2^2;
sbit COUNTER=P1^0;
sbit SCAN=P1^1;
sbit DONE=P1^2;
sbit R_led4=P2^6;
sbit R_led5=P2^1;

unsigned char RFID_CHK,Current,Data[4],flag[4]={0,0,0,0},a,b,c;
unsigned int temp;

void buzzer(unsigned char loop, int tone)
{
   unsigned char beep;
   for(beep=0;beep<loop;beep++)
   {
      buz=1;
      LCD_delay(tone);
      buz=0;
      LCD_delay(tone);
   }
}

void Send_Char(unsigned char buf)
{
	SBUF = buf;
	while(TI == 0);
	TI = 0;
}

 void send_string(char code *ptr)
{
	while (*ptr != 0)
	{
		SBUF = *ptr;
		ptr++;
		while (TI == 0);
		TI = 0;
		LCD_delay(1);
	}

}

void warn()
{
buzzer(5,50);      
 
     	send_string("at+cmgf=1\n");   
      Send_Char(0x0d);
      Send_Char(0x0a);
      LCD_delay(100);
	   send_string("at+cmgs=\"+919566080362\"\n");
      Send_Char(0x0d);
      Send_Char(0x0a);
      LCD_delay(100);
      LCD_delay(5);
	   send_string(" Unauthorized Logistic Movement \n");
      Send_Char(26);
      LCD_delay(1000);
      LCD_delay(1000);
      
buzzer(5,50);
  send_string("atd9566080362;\n");
      Send_Char(0x0d);
      Send_Char(0x0a);
      LCD_delay(5);
      LCD_delay(5);      
}

void Ex0(void) interrupt 0
{
	Data[(25 - Current) / 8] &= ~(0x1 << ((25 - Current) % 8));
	TH0 = 0xEE;
	TL0 = 0x0;
	Current++;
}

void Ex1(void) interrupt 2
{
	Data[(25 - Current) / 8] |= 0x1 << ((25 - Current) % 8);
	TH0 = 0xEE;
	TL0 = 0x0;
	Current++;
}

void Timer0(void) interrupt 1
{
	TH0 = 0xEE;
	TL0 = 0x0;
	Current = 0;
}

unsigned char Send_Data(void)
{
if(Data[1] == 139)
{
return 1;
}

if(Data[1] == 196)
{
return 2;
}

if(Data[1] == 82)
{
return 3;
}
//return 0;
} 

void main (void)
{
	SCON = 0x50;
	TMOD = 0x21;
	IE   = 0x87;
	TH0  = 0xEE;
	TL0  = 0x0;
	P2=0;
	P1=0;
	//  9600
	TH1  = 0xFD;
	TL1  = 0xFD;
	TCON = 0x55;
   	LCD_init();
  	LCD_row1();
	LCD_puts("  RFID Based   ");
  	LCD_row2();
	LCD_puts(" LOGISTIC MGMT ");
  	LCD_delay(10);
	while(1)
	{ 
		if(Current == 26)
		{
			Current = 0;
			EA = 0;			
			RFID_CHK = 0;
			RFID_CHK = Send_Data();	
		//Send_Char(0x30+RFID_CHK);
			if(flag[RFID_CHK] == 0)
			{
  				LCD_clear();
  				LCD_row1();
				LCD_puts("  ALERT!!!! ");
				warn();
			//	buzzer(8,250); 
  				LCD_clear();
  				LCD_row1();
				LCD_puts("  RFID Based   ");
  				LCD_row2();
				LCD_puts(" LOGISTIC MGMT ");
  				LCD_delay(1);
  			}
		
		EA = 1;
		}

		if(SCAN==SELECT)
		{
			a=0;
			b=0;
			c=0;
			LCD_clear();
  			LCD_row1();
  			LCD_puts("ACCOUNTING MODE");
  			LCD_row2();
  			LCD_puts("SHOW UR PRODUCT");
  			LCD_delay(5);
			while(DONE!=SELECT)
			{	
				if(Current == 26)
				{
					Current = 0;
					EA = 0;
					RFID_CHK = Send_Data();	

					if(RFID_CHK == 1)
					{
						a++;
					}
					else if(RFID_CHK == 2)
					{
						b++;
					}
					else if(RFID_CHK == 3)
					{
						c++;
					}
					RFID_CHK = 0;
					EA = 1;		
				}
			}
			LCD_clear();
  				LCD_row1();
				LCD_puts("Product A ");
				LCD_putc(0x30 + (a/10));
				LCD_putc(0x30 + (a%10));
				LCD_row2();
				LCD_puts("B ");
				LCD_putc(0x30 + (b/10));
				LCD_putc(0x30 + (b%10));

				LCD_puts("  C ");
				LCD_putc(0x30 + (c/10));
				LCD_putc(0x30 + (c%10));
  				LCD_delay(1000);
				LCD_clear();
  				LCD_row1();
				LCD_puts("  RFID Based   ");
  				LCD_row2();
				LCD_puts(" LOGISTIC MGMT ");
  				LCD_delay(1);
		}
 
		if(COUNTER==SELECT)
		{
			LCD_clear();
  			LCD_row1();
  			LCD_puts("COUNTER MODE");
  			LCD_row2();
  			LCD_puts("SHOW UR PRODUCT");
  			LCD_delay(500);
			while(DONE!=SELECT)
			{
				if(Current == 26)
				{
					Current = 0;
					EA = 0;
					RFID_CHK = Send_Data();	

					if(flag[RFID_CHK] == 0)
					{
						LCD_row2();
		 	 			LCD_puts("Product ");
						LCD_putc(0x30 + RFID_CHK);
			 			LCD_puts(" Sold   ");
			 			flag[RFID_CHK]=1;
					}
					RFID_CHK = 0;
					EA = 1;		
				}	
			}
			LCD_clear();
  				LCD_row1();
				LCD_puts("  RFID Based   ");
  				LCD_row2();
				LCD_puts(" LOGISTIC MGMT ");
  				LCD_delay(10);
		}
	}
}

